// pages/studyRecord/essaywork/essaywork.js
const app = getApp();
const httpUtils = require('../../../utils/httpUtils.js');
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1, //	type 1今日作业，2往期作业
    tabs: ["未完成作业", "已完成作业"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    homeworkList:[],
    page: 1,
    pageSize: 30,
    hasMoreData: false,
    imageurl: 'https://yxpmaterial.oss-cn-beijing.aliyuncs.com/',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this;

     //设置tab
     wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
  },


  //点击某个Tab
  tabClick: function (e) {
    var that = this;
    that.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });

    if (that.data.activeIndex == 0) {
      that.setData({
        type: 1,
        page: 1,
      })
    } else if (that.data.activeIndex == 1) {
      that.setData({
        type: 2,
        page: 1,
      })
    }
    //获取作业列表
    that.getHomeWorkRecords();
  },

 //获取作业列表
  getHomeWorkRecords:function(){
    // getEssayHomeWork
    let that = this;
    wx.showLoading({
      title: '',
      mask: true
    })

    httpUtils.getEssayHomeWork({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      type: that.data.type,
      page: that.data.page,
      size: that.data.pageSize
    }).then((res) => {
      var homeworkList = that.data.homeworkList;
      let list = res.data.list;

      if (that.data.page == 1) {
        homeworkList = []
      }

      if (list.length < that.data.pageSize) {
        that.setData({
          homeworkList: homeworkList.concat(list),
          hasMoreData: false
        })
      } else {
        that.setData({
          homeworkList: homeworkList.concat(list),
          hasMoreData: true,
          page: that.data.page + 1
        })
      }

    });
  },

  // 点击每个条目去上传作业
  gotowriting:function(e){
    let essay_id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/studyRecord/writing/writing?essay_id=' +essay_id+"&source=1",
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  var that = this;
    that.setData({
      page: 1,
      homeworkList: [],
    })
    console.log("onShow-type:" + that.data.type);
    that.getHomeWorkRecords();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasMoreData) {
      that.getHomeWorkRecords();
      console.log("请求下一页数据")
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})